package com.example.gokchinesefoodmappcdev.service.impl;

import com.baomidou.mybatisplus.extension.api.R;
import com.example.gokchinesefoodmappcdev.dao.LocationAddressDao;
import com.example.gokchinesefoodmappcdev.dao.OrderInfoDao;
import com.example.gokchinesefoodmappcdev.entity.*;
import com.example.gokchinesefoodmappcdev.entity.dto.OrderInfoDTO;
import com.example.gokchinesefoodmappcdev.entity.dto.OrderInfoExcelDTO;
import com.example.gokchinesefoodmappcdev.service.OrderInfoService;
import com.example.gokchinesefoodmappcdev.util.ResultData;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class OrderInfoServiceImpl implements OrderInfoService {

    @Autowired
    OrderInfoDao orderInfoDao;

    @Autowired
    LocationAddressDao locationAddressDao;


    /*
    * 查询订单退款列表的信息
    * */
    @Override
    public ResultData selectOrderReturnList(Integer pageSize, Integer pageNumber, String returnNo) {

        //开启分页,该方法调用需要在调用数据库之前
        PageHelper.startPage(pageNumber,pageSize);

        List<OrderReturn> orderReturns = orderInfoDao.selectOrderReturnList(returnNo);

        PageInfo<OrderReturn> pageInfo=new PageInfo<>(orderReturns);

        ResultData resultData=new ResultData();
//        resultData.put("code",0);

        //构建翻页对象

        resultData.put("pageNo",pageInfo.getPageNum());
        resultData.put("pageSize",pageInfo.getPageSize());
        resultData.put("total",pageInfo.getTotal());
        resultData.put("totalPages",pageInfo.getPages());
        resultData.put("first",pageInfo.getPageNum());


        //将翻页对象存入结果集

        resultData.put("rows",orderReturns);

        return resultData;


    }

    /*
    * 查询出所有订单信息，生成excel
    * */
    @Override
    public List<OrderInfoExcelDTO> getOrdersInfo() {

        List<OrderInfoExcelDTO> ordersInfo = orderInfoDao.getOrdersInfo();
        return ordersInfo;
    }

    /*
    * 订单发货
     *     根据订单号，修改发货状态（2：已发货），发货时间，填上物流编号，物流公司，
     *            注：生成一个现在的时间来修改发货时间，
    * */
    @Override
    public ResultData deliveryOrder(String orderNo, String logisticsNumber, String logisticsCompany) {
        Date deliveryTime = new Date();
        int i = orderInfoDao.deliveryOrder(orderNo, logisticsNumber, logisticsCompany, deliveryTime);
        if(i>0){
            return new ResultData(0,"操作成功!");
        }
        return new ResultData(500,"操作失败");

    }

    /*
    * 根据传入的订单号，查询子订单，信息，返回一个list集合
    * */
    @Override
    public ResultData selectListSubOrder(String orderNo) {
        List<OrderSku> orderSkus = orderInfoDao.selectListSubOrder(orderNo);
        if(orderSkus!=null){
            ResultData resultData=new ResultData();
            resultData.put("code",0);
            resultData.put("rows",orderSkus);
            return resultData;
        }
        return new ResultData(ResultData.OPERATION_FAIL,"没有找到该订单号的子订单，请检查订单号！");
    }

    /*
    * 根据传入的地址ID和订单号，查询订单信息，
    *                 包括用户住址信息，子订单信息，该订单的基本信息
    * */
    @Override
    public ResultData selectOrderInfoSByAddressAndOrderNo(int addressId, String orderNo) {
        //根据addressId查询订单上的用户住址信息
        LocationAddress addressInfo = locationAddressDao.selectAddressInfoById(addressId);
        Map<String,Object> rows=new HashMap<>();
        rows.put("address",addressInfo);
        ResultData resultData=new ResultData();
        resultData.put("code",0);

        List<OrderInfoDTO> orderInfoDTOS = orderInfoDao.selectSubOrdersByOrderNo(orderNo);
        rows.put("subOrders",orderInfoDTOS);

        //根据订单号查询订单信息
        OrderInfo orderInfo = orderInfoDao.selectOrderInfoByOrderNo(orderNo);
        rows.put("basicInfo",orderInfo);

        resultData.put("rows",rows);

        return resultData;
    }

    /*
    * 根据订单号查询用户住址ID
    * */
    @Override
    public int selectAddressIdByOrderNo(String orderNo) {
        int i = orderInfoDao.selectAddressIdByOrderNo(orderNo);
        return i;
    }

    /*
    * 查询订单列表
    * */
    @Override
    public ResultData selectOrderList(Integer pageNumber, Integer pageSize) {

        //开启分页,该方法调用需要在调用数据库之前
        PageHelper.startPage(pageNumber,pageSize);

        List<OrderInfo> orderInfos = orderInfoDao.selectOrderList();

        PageInfo<OrderInfo> pageInfo=new PageInfo<>(orderInfos);

        ResultData resultData=new ResultData();
        resultData.put("code",0);

        //构建翻页对象

        Map<String, Object> rows = new HashMap<>();

        rows.put("pageNo",pageInfo.getPageNum());
        rows.put("pageSize",pageInfo.getPageSize());

        //将翻页对象存入结果集

        rows.put("rows",orderInfos);

        resultData.put("rows",rows);

        return resultData;

    }
}
